package cn.shengchao.wecom.wxcp.acl.adapters.repositories;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.shengchao.examstar.rpc.wecom.dto.WeComDeptUserDto;
import cn.shengchao.wecom.wxcp.acl.ports.repositories.ICpUserRepository;
import cn.shengchao.wecom.wxcp.infrastructure.dao.CpUserDao;
import cn.shengchao.wecom.wxcp.infrastructure.dao.po.CpUser;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.cp.bean.user.WxCpDeptUserResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: examstarapi
 * @description: 企微成员
 * @author: Xiong weiqiang
 * @create: 2025-04-25 18:23
 */
@Service
@RequiredArgsConstructor
public class CpUserRepository implements ICpUserRepository {

    private final CpUserDao cpUserDao;
    @Transactional
    @Override
    public void checkSave(List<WxCpDeptUserResult.DeptUserList> deptUserDtoList) {
        if (CollUtil.isNotEmpty(deptUserDtoList)){
            List<String> userIds=deptUserDtoList.stream().map(WxCpDeptUserResult.DeptUserList::getUserId).toList();
            List<CpUser> cpUserList=cpUserDao.selectList(Wrappers.lambdaQuery(CpUser.class).in(CpUser::getUserId,userIds));
            List<String> dbUserIds=new ArrayList<>();
            if (CollUtil.isNotEmpty(cpUserList)){
                dbUserIds=cpUserList.stream().map(CpUser::getUserId).toList();
            }
            List<String> finalDbUserIds = dbUserIds;
            List<WxCpDeptUserResult.DeptUserList> newDeptUserDtoList=deptUserDtoList.stream().filter(item->!finalDbUserIds.contains(item.getUserId())).toList();
            if (CollUtil.isNotEmpty(newDeptUserDtoList)){
                newDeptUserDtoList.stream().forEach(item->{
                    CpUser cpUser=new CpUser();
                    cpUser.setUserId(item.getUserId());
                    cpUser.setName(item.getUserId());
                    cpUser.setMainDepartment(item.getDepartment().toString());
                    cpUser.setLoadStatus(0);
                    cpUser.setLastUpdateTime(new Date());
                    cpUser.setStatus(1);
                    cpUserDao.insert(cpUser);
                });
            }
        }
    }
    @Transactional
    @Override
    public void messageSaveUser(WeComDeptUserDto user) {
        List<CpUser> qywxUsers=cpUserDao.selectList(Wrappers.lambdaQuery(CpUser.class)
                .eq(CpUser::getUserId,user.getUserId())
                .eq(CpUser::getStatus,1)
        );
        if (CollUtil.isEmpty(qywxUsers)){
            CpUser qywxUser=new CpUser();
            BeanUtil.copyProperties(user,qywxUser);
            qywxUser.setAsFollower(1);
            qywxUser.setLoadStatus(0);
            qywxUser.setLastUpdateTime(new Date());
            cpUserDao.insert(qywxUser);
        }
    }

    @Override
    public void updateUserNewId(String userId, String newUserId) {
        List<CpUser> qywxUsers=cpUserDao.selectList(Wrappers.lambdaQuery(CpUser.class)
                .eq(CpUser::getUserId,userId)
                .eq(CpUser::getStatus,1)
        );
        if (CollUtil.isNotEmpty(qywxUsers)){
            cpUserDao.update(Wrappers.lambdaUpdate(CpUser.class)
                    .set(CpUser::getUserId,newUserId)
                    .eq(CpUser::getUserId,userId)
                    .eq(CpUser::getStatus,1)
            );
        }
    }

    @Override
    public void deleteUser(String userId) {
        cpUserDao.update(Wrappers.lambdaUpdate(CpUser.class)
                .set(CpUser::getStatus,5)
                .eq(CpUser::getUserId,userId)
                .eq(CpUser::getStatus,1)
        );
    }
}